package com.xbk.platform;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import com.xbk.util.StringUtil;

public class DataMap {
	
	private Map<String, Object> map = new HashMap<String, Object>();
	
	public DataMap put(String name,Object value){
		this.map.put(name, value);
		return this;
	}
	
	public void clear(){
		this.map.clear();
	}
	
	public Object get(String name){
		return this.map.get(name);
	}
	
	public String getString(int index){
		String sReturn = "";
		Object value = this.map.get(this.getKeyList().get(index - 1));
		if(value != null) sReturn = value.toString();
		return sReturn;
	}
	
	public String getString(String name){
		String sReturn = "";
		Object value = this.map.get(name);
		if(value != null) sReturn = value.toString();
		return StringUtil.getNull(sReturn);
	}
	
	public String getString(String name,String defaultStr){
		String sReturn = "";
		Object value = this.map.get(name);
		if(value != null) sReturn = value.toString();
		else sReturn = defaultStr;
		return sReturn;
	}
	
	public double getDouble(String name) throws Exception{
		double d = 0d;
		Object value = this.map.get(name);
		if(value != null) {
			try{
			d = Double.parseDouble(value.toString());
			} catch (Exception e){
				throw e;
			}
		}
		return d;
	}
	
	public int getInteger(String name) throws Exception{
		int i = 0;
		Object value = this.map.get(name);
		if(value != null) {
			try{
				i = Integer.parseInt(value.toString());
			} catch (Exception e){
				throw e;
			}
		}
		return i;
	}
	
	public ArrayList<String> getKeyList(){
		ArrayList<String> list = new ArrayList<String>();
		list.addAll(this.map.keySet());
		return list;
	}

	public int size() {
		return this.map.size();
	}

	public void remove(String dataName) {
		this.map.remove(dataName);
	}

	@Override
	public String toString() {
		return "DataMap [map=" + map + "]";
	}
}
